package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/8

*/
public class WebThead365 extends Thread {
    String string;
    Document doc;

    WebThead365(String str, Document docc){
        string= str;
        doc= docc;
    }
    private static Logger logger = Logger.getLogger(WebThead365.class);

    synchronized public void run() {
        Houses.lock.lock();
        try {

            Elements listCon = doc.getElementsByClass("z-list-item");

        for (Element element : listCon) {
            Thread.sleep(1000);
            //获取房子的图片链接
            String imgUrl = element.getElementsByClass("z-list-item-img").get(0).getElementsByTag("img").attr("data-original");
            //获取房子的具体信息链接
            String houseSingleUrl = element.getElementsByClass("z-fl z-text-overflow").get(0).getElementsByTag("a").attr("href");
            String title = element.getElementsByClass("z-fl z-text-overflow").get(0).child(0).html().replace("·","");

            String replace = element.getElementsByClass("z-list-type-box").get(0).html().replace("<span>|</span>", "");
            String[] info = replace.split("\n");
            String singleType = "";
            String size = "";
            String direcType = "";
            String singleLayer = "";
            String rentType = "";
            if (info.length == 6){
                //获取房型
                singleType = info[1];
                //获取面积
                size = info[2];
                //获取楼层
                direcType = info[4];
                //获取出租类型
                singleLayer = info[5];
                rentType = info[0];
            }else{
                //获取房型
                singleType = info[0];
                //获取面积
                size = info[1];
                //获取楼层
                direcType = info[3];
                //获取出租类型
                singleLayer = info[4];
                rentType = info[0];
            }
//https://saas-qw2.qfangimg.com/pro/032e22a2-e37d-464a-a4e8-750c8c0ff25f.jpg-240x180^/rent/100267227?insource=rent_list&top=1744^天坛新村 2室1厅1厨1卫 58.0m² 整租^2室1厅^58㎡^简装^中层(共7层)^整租^秦淮-月牙湖-天坛新村^空房^南京市月牙湖小学^2400
            String place = Html2Text.getContent(element.getElementsByClass("z-list-address").get(0).child(0).html());
            String underPlace = element.getElementsByClass("z-list-address").get(0).child(1).html();
            //房子的价格
            String price = element.getElementsByClass("z-list-price").get(0).child(0).html().replace("<span>","").replace("</span>","");
            //输出信息到日志
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(imgUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(houseSingleUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(title.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(size.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(direcType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleLayer.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(rentType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(place.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(underPlace.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(price.replace("\t","").replace("\r","").replace("\n",""));
            logger.info(stringBuilder.toString());
        }
        }catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            Houses.lock.unlock();    	//释放锁
        }



    }
}
